package com.developer.platform.application.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.developer.platform.application.query.assembler.MenuAssembler;
import com.developer.platform.application.query.dto.MenuListDto;
import com.developer.platform.application.query.dto.MenuParentDto;
import com.developer.platform.application.query.dto.MenuTreeDto;
import com.developer.platform.application.query.dto.RoleMenuDto;
import com.developer.platform.domain.Menu;
import com.developer.platform.infrastructure.data.mapper.MenuMapper;
import com.developer.platform.infrastructure.data.mapper.RoleMenuMapper;
import com.developer.platform.infrastructure.enums.MenuEnum;
import com.developer.platform.infrastructure.enums.StatusEnum;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Component
public class MenuQueryManager {

    private final MenuMapper menuMapper;
    private final MenuAssembler menuAssembler;
    private final RoleMenuMapper roleMenuMapper;

    public MenuQueryManager(MenuMapper menuMapper,
                            MenuAssembler menuAssembler,
                            RoleMenuMapper roleMenuMapper) {
        this.menuMapper = menuMapper;
        this.menuAssembler = menuAssembler;
        this.roleMenuMapper = roleMenuMapper;
    }

    /**
     * 查询全部资源
     *
     * @author ：qiaoliang

     * @return java.util.List<com.microorganism.platform.application.query.dto.MenuListDto>
     * @date 2020-10-30 15:30
     */
    public List<MenuListDto> list(String title, String component, StatusEnum status){
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(title),Menu::getTitle, title);
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(component),Menu::getComponent, component);
        lambdaQueryWrapper.eq(Objects.nonNull(status),Menu::getStatus, status);
        lambdaQueryWrapper.orderByAsc(Menu::getSort);
        return menuAssembler.toMenuListDtoPage(menuMapper.selectList(lambdaQueryWrapper));
    }

    /**
     * 查询可分配资源
     *
     * @author ：qiaoliang

     * @return java.util.List<com.microorganism.platform.application.query.dto.MenuListDto>
     * @date 2020-10-30 15:30
     */
    public List<MenuParentDto> parent(){
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.and(l->l.eq(Menu::getType, MenuEnum.M).or().eq(Menu::getType, MenuEnum.C));
        lambdaQueryWrapper.eq(Menu::getStatus, StatusEnum.Y);
        lambdaQueryWrapper.orderByAsc(Menu::getSort);
        return menuAssembler.toMenuParentDto(menuMapper.selectList(lambdaQueryWrapper));
    }

    /**
     * 查询tree结构菜单
     *
     * @author ：qiaoliang

     * @return java.util.List<com.microorganism.platform.application.query.dto.RoleMenuDto>
     * @date 2020-11-04 13:40
     */
    public List<MenuTreeDto> getMenuTree(){
        LambdaQueryWrapper<Menu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.isNotNull(Menu::getType);
        lambdaQueryWrapper.eq(Menu::getStatus, StatusEnum.Y);
        lambdaQueryWrapper.orderByAsc(Menu::getSort);
        return menuAssembler.toMenuTreeDto(menuMapper.selectList(lambdaQueryWrapper));
    }

    /**
     * 通过角色ID获取角色下菜单，及全部菜单
     *
     * @author ：qiaoliang
     * @param roleId :
     * @return com.microorganism.platform.application.query.dto.RoleMenuDto
     * @date 2020-11-09 17:54
     */
    public RoleMenuDto getRoleMenuDto(long roleId){
        return menuAssembler.toRoleMenuDto(getMenuTree(), roleMenuMapper.findByRoleId(roleId));
    }

}
